//
//  ZJCSettingViewController.m
//  ZJCLoveBee
//
//  Created by 朱建超 on 16/12/22.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

#import "ZJCSettingViewController.h"
#import "Masonry.h"
#import "AIMineCacheCell.h"
#import "AIMineAboutMyController.h"

static NSString *cellID = @"cellID";
static NSString *cacheCellID = @"cacheCellID";

@interface ZJCSettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,weak)UILabel *label;

@end

@implementation ZJCSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;
    
    self.title = @"设置";
    
    //搭建界面
    [self setupUI];
    
    UILabel *label = [UILabel new];
    
    self.label = label;
    label.backgroundColor = [UIColor greenColor];
    
    
    
}

-(void)setupUI{

    UITableView *tableView = [[UITableView alloc] init];
    
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 60;
    tableView.tableFooterView = [[UIView alloc] init];
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [tableView registerClass:[AIMineCacheCell class] forCellReuseIdentifier:cacheCellID];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.mas_equalTo(self.view);
        
    }];

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return 10;
    }else{
        return 20;
    }

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    switch (section) {
        case 0:
            return 2;
        case 1:
            return 1;
        default:
            return 0;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
            
            cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.textLabel.text = @"关于小熊";
            
        }else{
        
            AIMineCacheCell *cacheCell = [tableView dequeueReusableCellWithIdentifier:cacheCellID forIndexPath:indexPath];
            
            cacheCell.cacheText = [self getCacheSize];

            return cacheCell;
            
        }
        
    }
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

        cell.textLabel.text = @"退出当前账号";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        
        return cell;

}

#pragma mark - 计算缓存大小
-(NSString *)getCacheSize{

    //定义变量存储总的缓存大小
    long long sumSize = 0;
    
    //1、获取当前图片缓存路径
    NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    
    //2、创建文件管理对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //获取当前缓存路径下的所有子路径
    NSArray *subPaths = [fileManager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
    
    //遍历所有子文件
    for (NSString *subPath in subPaths) {
        
        //1)、拼接完整的路径
        NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
        
        //2)、计算文件的大小
        long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
        
        //3)、加载到文件的大小
        sumSize += fileSize;
        
    }
    
    float size_m = sumSize / (1000 * 1000);
    
    return [NSString stringWithFormat:@"%.2fm",size_m];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        
        if (indexPath.row == 1) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"缓存清除" message:@"确定清除缓存？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            
            [alertView show];
            
        }else{
        
            AIMineAboutMyController *aboutMyController = [[AIMineAboutMyController alloc] init];
            
            [self.navigationController pushViewController:aboutMyController animated:YES];
        
        }
        
    }

}

#pragma mark - UIAlertView的方法实现
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    //判断点击的是确认案件
    if (buttonIndex == 1) {
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
        
        [fileManager removeItemAtPath:cacheFilePath error:nil];
        
        [self.tableView reloadData];
        
    }

}



@end
